package timing.ukulele.portal.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import timing.ukulele.portal.persistent.User;
import timing.ukulele.common.Constant;
import timing.ukulele.common.ResponseResult;
import timing.ukulele.portal.persistent.System;
import timing.ukulele.portal.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/user")
@Api("用户接口")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private PasswordEncoder passwordEncoder;

    @GetMapping("/page")
    @ApiOperation("分页获取用户列表")
    public ResponseResult<IPage<User>> page(
            @ApiParam(value = "查询用户名", required = true) @RequestParam String username,
            @ApiParam(value = "查询名称", required = true) @RequestParam String name,
            @ApiParam(value = "当前页码", required = true, example = "1") @RequestParam Integer pageNum,
            @ApiParam(value = "每页大小", required = true, example = "10") @RequestParam Integer pageSize,
            @ApiParam(value = "系统权限ID") @RequestHeader(Constant.SYSTEM) String systemIds) {
        HashMap<String, Object> map = new HashMap<>();
        if (username != null) {
            map.put("username", username);
        }
        if (name != null) {
            map.put("name", name);
        }
        return ResponseResult.success(userService.getPage(map, systemIds, pageNum, pageSize));
    }

    @PostMapping("/create")
    @ApiOperation("保存用户")
    @ApiImplicitParam(name = "user", value = "用户对象", required = true, dataType = "User")
    public ResponseResult<User> create(
            @ApiParam(value = "用户对象", required = true) @RequestBody User user,
            @RequestHeader(Constant.USERNAME) String username) {
        user.setCreateBy(username);
        user.setPassword(passwordEncoder.encode("123456"));
        if (user.getRoleId() == null || user.getRoleId() <= 0) {
            user.setRoleId(3);
        }
        userService.save(user);
        User newUser = userService.getByUsername(user.getUsername());
        userService.saveUserSystem(newUser.getId(), user.getSystemIds(), new HashMap<>());
        return ResponseResult.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改用户信息")
    @ApiImplicitParam(name = "use", value = "用户对象", required = true, dataType = "User")
    public ResponseResult<User> update(@ApiParam("用户对象") @RequestBody User user,
                                       @RequestHeader(Constant.USERNAME) String username) {
        user.setCreateBy(username);
        user.setPassword(null);
        userService.updateById(user);
        userService.saveUserSystem(user.getId(), user.getSystemIds(), new HashMap<>());
        return ResponseResult.success();
    }

    @PostMapping("/validateUsername")
    @ApiOperation("校验用户名是否存在")
    public ResponseResult<Boolean> validateUsername(
            @ApiParam("用户名") @RequestParam String username,
            @ApiParam("更新用户的ID") @RequestParam Long updateUserId) {
        User user = userService.getByUsername(username);
        if (user != null) {
            if (user.getId().equals(updateUserId)) {
                return ResponseResult.success(true);
            }
            return ResponseResult.success(false);
        }
        return ResponseResult.success(true);
    }

    @PostMapping("/updatePassword")
    @ApiOperation("修改用户密码")
    public ResponseResult<Boolean> updatePassword(@ApiParam("旧密码") @RequestParam String oldPassword,
        @ApiParam("新密码") @RequestParam String newPassword, @ApiParam("修改密码用户的ID") @RequestParam Long updateUserId) {
        if (!userService.updatePassword(oldPassword, passwordEncoder.encode(newPassword), updateUserId)) {
            return ResponseResult.error("旧密码输入有误!");
        }
        return ResponseResult.success();
    }

    @GetMapping("/getUserInfo")
    @ApiOperation(value = "获取用户信息")
    public ResponseResult<User> getUerInfo(@RequestHeader(Constant.USERNAME) String username) {
        return ResponseResult.success(userService.getByUsername(username));
    }

    @GetMapping("/systemList")
    @ApiOperation(value = "获取当前用的权限系统")
    public ResponseResult<List<System>> getAuthSystemList(@RequestHeader(Constant.USERNAME) String username) {
        return ResponseResult.success(userService.getUserSystemList(username));
    }
}
